﻿using UnityEngine;

public class ColliderCore : MonoBehaviour
{

    public Transform m_LogicParent; // 绑定的点，位置和方向要按照这个节点来进行更新
    public float m_fLive = -1; // 生存时间,  如果是-1，就需要跟着父节点的删除而删除，比如子弹
    public Color m_Color = Color.green;
    public SmartTimer m_DelayShow = new SmartTimer(); // 延时显示
    protected SmartTimer m_fLiveTimer = new SmartTimer();
    protected bool m_bShow = true; //是否画线框

    public Vector3 m_posOffset; // 相对的偏移量
    public float m_fRotate;
    bool m_bSymbiotic = false;
    protected Vector3 m_CurPos;
    protected Vector3 m_CurDir; // 为了防止逻辑节点没了，没有参数可用

    protected float m_fHeight = 1;
    void Start()
    {

    }
    public void SetHeight(float fHeight)
    {
        m_fHeight = fHeight;
    }

    public void SetFollow(bool follow)
    {
        m_bSymbiotic = follow;
        if (m_bSymbiotic)
            SetLive(-1); // 如果是共生的，就设定成递增计时器，不会自己删掉自己
    }
    public void SetLogicParent(Transform logic)
    {
        m_LogicParent = logic;
        gameObject.transform.parent = ColliderManager.Instance().GetRootTransform(); // 挂到通用点上
        gameObject.name = logic.gameObject.name;
        m_CurPos = CommonTools.MoveVector(m_LogicParent.transform.position, m_LogicParent.transform.forward, m_posOffset);
        m_CurDir = m_LogicParent.transform.forward;
        m_CurDir = CommonTools.RotateVector(m_CurDir, Vector3.up, m_fRotate);
    }
    public void DoSetOffset(Vector3 offset)
    {
        m_posOffset = offset;
    }
    public void DoSetRotate(float fRotate)
    {
        m_fRotate = fRotate;
    }
    public void DoSetColor(Color color)
    {
        m_Color = color;

    }
    public void DoSetDelay(float fDelay)
    {
        m_DelayShow.DoReset(fDelay);
        m_bShow = false;
    }
    public void SetLive(float fLive)
    {
        if (fLive < 0 && m_bSymbiotic)
        {
            m_fLiveTimer.DoReset(0.5f);
        }
        else
        {
            m_fLive = fLive;
            m_fLiveTimer.DoReset(m_fLive);
        }
        m_bDestorying = false;
    }

    bool m_bDestorying = false;
    void Update()
    {
        if (m_DelayShow.DoUpdate())
        {
            m_bShow = true;
        }
        if (m_bSymbiotic)
        {
            if (m_LogicParent == null && !m_bDestorying)
            {
                m_bDestorying = true;
                m_fLiveTimer.DoReset(0.5f);
            }
        }
        else
        {

        }
        if (m_fLiveTimer.DoUpdate())
        {
            gameObject.StopOrDestroy();
            return;
        }
        // 如果不是共生的，时间到了就删掉


        if (m_LogicParent != null)
        {
            m_CurPos = CommonTools.MoveVector(m_LogicParent.transform.position, m_LogicParent.transform.forward, m_posOffset);
            m_CurDir = m_LogicParent.transform.forward;
            m_CurDir = CommonTools.RotateVector(m_CurDir, Vector3.up, m_fRotate);
        }        //else if (m_bSymbiotic) // 如果逻辑节点没了，而且是绑定模式的。就删掉自己
        //{
        //    gameObject.StopOrDestroy();
        //    return;
        //}

    }

}
